package com.hanma.servlet;

import com.hanma.bean.Manager;
import com.hanma.bean.User;
import com.hanma.service.ManagerService;
import com.hanma.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet({"/manageLogin.do","/manageLoginAction.do","/manageIndex.do","/manageAdd.do","/manageAddAction.do"})
public class managerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        ManagerService service = new ManagerServiceImpl();
        HttpSession session = req.getSession();
        String path = req.getServletPath();
        req.setCharacterEncoding("utf-8");
        //转到管理员登录界面
        if(path.equals("/manageLogin.do")) {
            session.removeAttribute("mid");
            req.getRequestDispatcher("/manage/manager/login.jsp").forward(req, resp);
        }
        //提交管理员登录界面
        else if(path.equals("/manageLoginAction.do")) {
            if(session.getAttribute("mid") != null){
                Integer id = (Integer) session.getAttribute("mid");
                Manager manager = service.findAllById(id);
                req.setAttribute("manager", manager);
                req.getRequestDispatcher("/manage/index.jsp").forward(req, resp);
                return;
            }
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            Manager manager = service.findByNameAndPassword(username, password);
            if(manager == null) {
                req.getRequestDispatcher("/manage/manager/login.jsp").forward(req, resp);
                return;
            }
            session.setAttribute("mid", manager.getId());
            req.setAttribute("manager", manager);
            req.getRequestDispatcher("/list.do").forward(req, resp);
        }
        //跳转到管理员界面
        else if(path.equals("/manageIndex.do")) {
            Integer id = (Integer) session.getAttribute("mid");
            Manager manager = service.findAllById(id);
            if(manager.getPid() == 0) {
                List<Manager> managerList = service.findAll(manager.getId());
                req.setAttribute("manager", manager);
                req.setAttribute("managerList", managerList);
                req.getRequestDispatcher("/manage/manager/update.jsp").forward(req, resp);
                return;
            }
            req.setAttribute("manager", manager);
            req.getRequestDispatcher("/manage/manager/update.jsp").forward(req, resp);
        }
        //添加管理员
        else if(path.equals("/manageAdd.do")) {
            req.getRequestDispatcher("/manage/manager/add.jsp").forward(req, resp);
        }
        //提交添加管理员
        else if(path.equals("/manageAddAction.do")) {
            Integer pid = (Integer) session.getAttribute("mid");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            Manager manager = new Manager(pid, username, password);
            boolean flag = service.insert(manager);
            req.getRequestDispatcher("/manageIndex.do").forward(req, resp);
        }
    }
}
